const jwt = require('jsonwebtoken');
const crypto = require('crypto');
const {tokenSecret, tokenExpiresIn} = require('../config/index')

module.exports = {
    /**
     * sha1加密字符串
     * @return 返回加密后的字符串
     */
    createHash(s){
        let hash = crypto.createHash('sha1');
        hash.update(s);
        return  hash.digest('hex');
    },
    /**
     * 创建一个JWTtoken
     * @return 返回生成的token
     */
    createToken(userinfo) {
        return jwt.sign(userinfo, tokenSecret, {
            expiresIn : tokenExpiresIn
        });
    },
    /**
     * 验证JWTtoken
     * @return 生成返回true，失败返回false
     */
    verifyToken(token) {
        if(!token) {
            return false;
        }
        try {
            return jwt.verify(token, tokenSecret);
        } catch (e) {
            return false;
        }
    },
}
